using SDK.Scripts.Utils;

namespace SDK.Scripts.Plat
{
	public class GamePlatWin : BaseGamePlat
	{
		private string PLAT_PWD_KEY = "22eb4d3820015070";

		private string note102 = "guest={0}&channel={1}&gameId={2}&gameVersion={3}&time={4}&sign={5}&username={6}&pwd={7}";

		public override void Login(bool isGuest)
		{
			long timeStamp = Timer.GetTimeStamp();
			SDKPlatCfg cfg = base.Cfg;
			object text;
			if (cfg.userName == string.Empty)
			{
				text = Prefs.GetItem("plat_username", string.Empty);
			}
			else
			{
				SDKPlatCfg cfg2 = base.Cfg;
				text = cfg2.userName;
			}
			string text2 = (string)text;
			SDKPlatCfg cfg3 = base.Cfg;
			object text3;
			if (cfg3.pwd == string.Empty)
			{
				text3 = Prefs.GetItem("plat_password", string.Empty);
			}
			else
			{
				SDKPlatCfg cfg4 = base.Cfg;
				text3 = cfg4.pwd;
			}
			string text4 = (string)text3;
			int num = 1;
			if (text2 != string.Empty && text4 != string.Empty)
			{
				num = 2;
				text4 = MD5.GetMD5(PLAT_PWD_KEY + text4);
			}
			int guest = num;
			string pwd = text4;
			long time = timeStamp;
			string userName = text2;
			SDKPlatCfg cfg5 = base.Cfg;
			string secretSign = GetSecretSign(guest, pwd, time, userName, cfg5.appKey);
			string loginNote = string.Format(note102, num, base.Channel, base.GameId, base.GameVersion, timeStamp, secretSign, text2, text4);
			ReqWebLogin(loginNote);
		}

		public override void Pay(string payInfo)
		{
			OnPayBack(1, payInfo);
		}

		public override string DoAction(platActionID id, string str = "")
		{
			Debuger.LogDebug("action = " + id + ",msg = " + str);
			switch (id)
			{
			case platActionID.actionGetChannelId:
				return base.Channel;
			case platActionID.actionGet_WebCommomUrlStr:
				return $"userId={base.UserID}&gameId={base.GameId}&chargeGameId={base.GameId}&gameVersion={base.GameVersion}&appId={base.AppId}&supportType=1&channel={base.Channel}&apkVersion=100010&sdkVersion=100010&device=iphone-X&imei=&imsi=0&iccid=0&carrier=0&osInfo=&network=WIFI&radius=0&latitude=0&lontitude=0";
			default:
				return string.Empty;
			}
		}

		public string GetSecretSign(int guest, string pwd, long time, string userName, string appKey)
		{
			string strToEncrypt = $"{base.Channel}{base.GameId}{base.GameVersion}{guest}{pwd}{time}{userName}{appKey}";
			strToEncrypt = MD5.GetMD5(strToEncrypt);
			return strToEncrypt.ToLower();
		}

		private void ReqWebLogin(string loginNote)
		{
			string str = "https://wegame." + base.Domain + "/user/h5Login";
			str = str + "?" + loginNote;
			Debuger.LogDebug("req login web url = " + str);
			HttpClient.Request(str, string.Empty, LoginBack);
		}

		private void LoginBack(HttpClient.WebData data)
		{
			Debuger.LogDebug("web login back = " + data.msg);
			if (data.isSuccess)
			{
				OnLoginBack(1, data.msg);
			}
			else
			{
				OnLoginBack(0, data.msg);
			}
		}
	}
}
